package com.google.code.test;

import javax.script.*;

public class HelloWorld {

	String phpPrint = "print";
	public static final String T = ";";

	private String phpSelfPostURL() {
		String php = "$_POST ? $_SERVER['PHP_SELF'] : 'NEED TO POST A FORM FIRST!'";

		return php;
	}

	/**
	 * Translate to PHP codes.
	 * 
	 * @param statement
	 *            in JavaScript only
	 * @return
	 */
	private String toPhp(String jsStatement) {
		String retVal = jsStatement;

		if (jsStatement != null) {
			retVal = jsPrint2Php(jsStatement);
			retVal = jsBoolean2Php(jsStatement);
		}

		return retVal;
	}

	public String jsString2Php(String jsStatement) {
		String retVal = jsStatement;
		String temp = null;
		if (jsStatement != null && jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "strlen");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "trim");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "strtolower");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "strtoupper");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "nl2br");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "htmlentities");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "html_entity_decode");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "strpos");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "stripos");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "strrpos");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "strripos");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "str_replace");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "str_ireplace");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "stripslashes");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "strip_tags");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "is_numeric");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "ctype_digit");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "ctype_alnum");
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("print") == 0) {
			retVal = jsStatement.replaceAll("print", "ctype_alpha");
		}

		return retVal;
	}

	public String jsPrint2Php(String jsStatement) {
		String retVal = jsStatement;
		if (jsStatement != null && jsStatement.trim().indexOf("print") == 0) {
			// retVal = jsStatement.replaceAll("\\Q" + "print" + "\\E", "echo");
			retVal = jsStatement.replaceAll("print", "echo");
		}
		return retVal;
	}

	private String jsBoolean2Php(String jsStatement) {
		String retVal = jsStatement;
		if (jsStatement != null && jsStatement.trim().indexOf("TRUE") == 0) {
			// do nothing, php is also TRUE
		} else if (jsStatement != null
				&& jsStatement.trim().indexOf("FALSE") == 0) {
			// do nothing, php is also FALSE
		}
		return retVal;
	}

	/**
	 * Wrap the php array with array_key_exists function.
	 * 
	 * @param array
	 *            e.g. $_GET (without the array [ and ])
	 * @param key
	 *            e.g. 'bg' (with the proper quotes)
	 * @param keyNotFoundValue
	 *            e.g. "#FFFFFF" (with the proper quotes)
	 * @return the string wrapped with array_key_exists
	 */
	private String phpSafeArray(String array, String key,
			String keyNotFoundValue) {
		String retVal = null;

		if (array != null && !"".equals(array) && key != null
				&& !"".equals(key) && keyNotFoundValue != null
				&& !"".equals(keyNotFoundValue)) {
			retVal = "$temp = array_key_exists(" + key + ", " + array + ")? "
					+ array + "[" + key + "]: " + keyNotFoundValue + T;
		}

		return retVal;
	}

	/*
	 * If enclosed variable exists, must be enclosed with " not '.
	 */
	private String phpEnclosedVariable(String enclosedVariable) {
		String retVal = enclosedVariable;

		if (retVal != null) {
			if (retVal.indexOf("'") == 0) {
				// assuming if there is head, there will be a tail
				retVal = retVal.replaceAll("'", "\"");
			}
		}

		return retVal;
	}

	public static void main(String[] args) throws Exception {
		// HelloWorld h = new HelloWorld();
		// System.out.println(h.jsPrint2Php("print printprint"));

		// create a script engine manager
		ScriptEngineManager factory = new ScriptEngineManager();
		// create a JavaScript engine
		ScriptEngine engine = factory.getEngineByName("JavaScript");
		// evaluate JavaScript code from String
		// engine.eval("print('Hello, World')");

		ScriptEngineManager factory2 = new ScriptEngineManager();
		ScriptEngine engine2 = factory2.getEngineByName("php");
		// engine2.eval("<?php echo 'Hello, World'?>");

	}

}